﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Alos.Entity;
using Alos.IRepository;
using PetaPoco;

namespace Alos.Repository
{
    public class GroupInfoRepository : BaseRepository<GroupInfo>, IGroupInfoRepository
    {
        /// <summary>
        /// 根据教师ID和班级ID获取分组信息
        /// </summary>
        /// <param name="TeacherId">教师ID</param>
        /// <param name="ClassId">班级ID</param>
        /// <returns></returns>
        public IEnumerable<dynamic> GetGroupInfo(int TeacherId, int ClassId)
        {
            var stusql = Sql.Builder.Append("  select id from SysUserInfo where RoleId=3 and ClassId=@0 ", ClassId);
            List<string> str = DBContext.Query<string>(stusql).ToList();
            string stuid="";
            int i = 0;
            foreach (var item in str)
            {
                i++;
                if (i==1)
                {
                    stuid = item; 
                }
                else
                {
                    stuid += "," + item;
                }
            }
            //var sql = Sql.Builder.Append("if not exists (select * from GroupInfo where TeacherId=@0 and ClassId=@1 )  begin insert into GroupInfo(TeacherId,ClassId,GroupName,StuId) values(@0,@1,'第一组',@2) end  select * from GroupInfo where TeacherId=@0 and ClassId=@1 ", TeacherId, ClassId, stuid);
            var sql = Sql.Builder.Append("if not exists (select * from GroupInfo where TeacherId=@0 and ClassId=@1 )  begin insert into GroupInfo(TeacherId,ClassId,GroupName,StuId) values(@0,@1,'第1组',@2) end ", TeacherId, ClassId, stuid);
            DBContext.Execute(sql);
            sql = Sql.Builder.Append(" select * from GroupInfo where TeacherId=@0 and ClassId=@1 ",TeacherId,ClassId);
            return DBContext.Query<dynamic>(sql);
        }
        /// <summary>
        /// 根据分组信息添加
        /// </summary>
        /// <param name="GroupInfo">分组信息</param>
        /// <returns></returns>
        public bool CreatGroupInfo(GroupInfo GroupInfo)
        {
            return Convert.ToInt32(GroupInfo.Insert()) > 0;
        }
        /// <summary>
        /// 根据分组信息删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool DeleteGroupInfo(int id)
        {
            var sql = Sql.Builder.Append("delete  from GroupInfo where id=@0",id);
            return DBContext.Execute(sql)>0;
        }
        /// <summary>
        /// 根据分组老师ID和班级ID删除
        /// </summary>
        /// <param name="TeacherId">老师ID</param>
        /// <param name="ClassId">班级ID</param>
        /// <returns></returns>
        public bool DeleteGroupInfoByTeachaerID(int TeacherId, int ClassId)
        {
            var sql = Sql.Builder.Append("delete  from GroupInfo where TeacherId=@0 and ClassId=@1", TeacherId, ClassId);
            return DBContext.Execute(sql) > 0;
        }
        /// <summary>
        /// 根据分组ID修改该组信息
        /// </summary>
        /// <param name="GroupInfo">分组信息</param>
        /// <returns></returns>
        public bool UpdateGroupInfo(GroupInfo GroupInfo)
        {
            var sql = Sql.Builder.Append("  update  GroupInfo set GroupName=@0 ,StuId=@1,Leader=@2 where ID=@3", GroupInfo.GroupName, GroupInfo.StuId, GroupInfo.Leader, GroupInfo.ID);
            return DBContext.Execute(sql) > 0;
        }
    }
}
